package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.FrontImgService;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/shownanny")
public class ShowGoodNannyController {
	
	@Autowired
	private FrontImgService frontImgService;
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, FrontImg bean, Pagination pagination) {
		//查询条件封装
		FrontImgCriteria criteria = new FrontImgCriteria();
		if ( bean != null) {
			criteria.setImgType(Entity.Value.eq(3));
		}
		//分页查询
		Pagination list = frontImgService.selectPage(criteria, pagination);
		List<FrontImg> frontImglist=frontImgService.selectList(criteria);
		request.setAttribute("pagination", list);
		request.setAttribute("bean", bean);
		return "shownanny/list";
	}
	
	/**
	 * 编辑跳转
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, int frontImgid) {
		FrontImg frontImg = frontImgService.selectOne(frontImgid);
		String imgDescribe=frontImg.getImgDescribe();
		String namecall=imgDescribe.split("#")[0];
		String describe=imgDescribe.split("#")[1];
		request.setAttribute("namecall", namecall);
		request.setAttribute("describe", describe);
		request.setAttribute("bean", frontImg);
		return "shownanny/edit";
	}
	/**
	 * 更新
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(HttpServletRequest request,FrontImg frontImg,String namecall,String imgDescribe,@RequestParam(value = "nannyfile", required = false) MultipartFile nannyfile) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			FrontImg getfrontImg=frontImgService.selectOne(frontImg.getId());
			/*
			 * 上传(以字节流的形式上传)
			 */
			if(nannyfile!=null){
				if(FileUtils.deleteFile(getfrontImg.getImgKey())){
					byte[] multipartbackfilebytes=nannyfile.getBytes();
					String nannyfileimg_key=FileUtils.uploadFileBybyte(multipartbackfilebytes);
					String nannyfileimg_url=FileUtils.getFileUrls(nannyfileimg_key);
					getfrontImg.setImgKey(nannyfileimg_key);
					getfrontImg.setImgUrl(nannyfileimg_url);
				}else{
					jsonResult.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
					return jsonResult;
				}
			}
			getfrontImg.setImgDescribe(namecall+"#"+imgDescribe);
			frontImgService.update(getfrontImg);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
		}
		return jsonResult;
	}
}
